﻿
Partial Class WebForm_SelfManageWorkHeader_SearchWorkHeader
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.InitData()
        End If
    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStatus.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    '編輯
    Protected Sub gv_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gv.RowUpdating
        Me.SelectedStatus = Me.ddlStatus.SelectedValue
        Dim key As DataKey = Me.gv.DataKeys(e.RowIndex)
        Me.FindDirtyHeaderID = key("FindDirtyHeaderID")
        RaiseEvent RowUpdating(Me, New EventArgs)
    End Sub
    '刪除
    Protected Sub gv_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gv.RowDeleting
        Dim key As DataKey = Me.gv.DataKeys(e.RowIndex)
        Me.FindDirtyHeaderID = key("FindDirtyHeaderID")
        RaiseEvent RowDeleting(Me, New EventArgs)
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            TryCast(e.Row.FindControl("lknDelete"), LinkButton).Enabled = IIf(DataBinder.Eval(e.Row.DataItem, "Active") Is DBNull.Value, True, False)
        End If
    End Sub
End Class

Partial Class WebForm_SelfManageWorkHeader_SearchWorkHeader
#Region "初始化資料"
    Private Sub InitData()
        Me.BindDDLStatus()
    End Sub
#End Region

#Region "繫結資料"
    Private Sub BindDDLStatus()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        dt = MyObj.GetCodeMappingData("WorkHeader", "Active")

        Me.ddlStatus.DataTextField = "Text"
        Me.ddlStatus.DataValueField = "Value"
        Me.ddlStatus.DataSource = dt
        Me.ddlStatus.DataBind()
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select a.*,b.RegionName+c.RegionName+a.Location as Address, wh.Active from FindDirtyHeader a")
        sql.AppendFormat(" Join Region b on Left(a.VillageID,3)=b.RegionID")
        sql.AppendFormat(" and b.RegionType='T'")
        sql.AppendFormat(" Join Region c on a.VillageID=c.RegionID ")
        sql.AppendFormat(" and c.RegionType='V' ")

        sql.AppendFormat(" left join WorkHeader wh on a.WorkHeaderID = wh.WorkHeaderID ")
        sql.AddWithValue("Active", Me.ddlStatus.SelectedValue)
        
        If Me.ddlStatus.SelectedValue = "Y" Then
            sql.AppendFormat(" where wh.Active = '{0}' ", Me.ddlStatus.SelectedValue)
        Else
            sql.AppendFormat(" where wh.Active is null ")
        End If

        sql.AppendFormat(" and a.CreateBy = '{0}' ", clsAuth.GetUserID())
        sql.AppendFormat(" order by a.CreateDate desc")

        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()

        RaiseEvent DataBound(Me, New EventArgs)
        Me.gv.SelectedIndex = -1
    End Sub
#End Region

#Region "屬性"
    Public Property SelectedStatus() As String
        Get
            Return ViewState("SelectedStatus")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedStatus") = value
        End Set
    End Property

    Public Property FindDirtyHeaderID() As String
        Get
            Return ViewState("FindDirtyHeaderID")
        End Get
        Set(ByVal value As String)
            ViewState("FindDirtyHeaderID") = value
        End Set
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return Me.gv.Rows.Count
        End Get
    End Property
#End Region

#Region "事件"
    Public Event RowUpdating(ByVal source As Object, ByVal e As EventArgs)
    Public Event RowDeleting(ByVal source As Object, ByVal e As EventArgs)
    Public Event DataBound(ByVal source As Object, ByVal e As EventArgs)
#End Region
End Class